package com.xhwl.logistics.converter;

import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.input.QueryVehicleAccessRecordStatisticsBO;
import com.xhwl.logistics.component.VehicleConfigProperty;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.dto.query.VehicleAccessRecordStatisticsDTO;
import com.xhwl.logistics.service.cent.QueryProjectService;
import com.xhwl.logistics.util.AboutDateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

//@Component
public class ToQueryVehicleAccessRecordStatisticsBO {

    @Autowired
    private VehicleConfigProperty vehicleConfigProperty;

    @Autowired
    private QueryProjectService queryProjectService;

    public QueryVehicleAccessRecordStatisticsBO toQueryVehicleAccessRecordStatisticsBO(VehicleAccessRecordStatisticsDTO vehicleAccessRecordStatisticsDTO) {
        QueryVehicleAccessRecordStatisticsBO queryVehicleAccessRecordStatisticsBO = new QueryVehicleAccessRecordStatisticsBO();

        String homeAddress = queryProjectService.queryProjectLocalPlateName(vehicleAccessRecordStatisticsDTO.getProjectId());
        queryVehicleAccessRecordStatisticsBO.setHomeAddress(homeAddress);

        Date date = vehicleAccessRecordStatisticsDTO.getTime();
        if (null==date) {
            date = new Date();
        }
        Integer type = vehicleAccessRecordStatisticsDTO.getType();
        Date startTime = vehicleAccessRecordStatisticsDTO.getStartTime();
        Date endTime = vehicleAccessRecordStatisticsDTO.getEndTime();

        Date handleDate;
        if (type.equals(0)) {
            handleDate = AboutDateUtil.theSameDayHourOfDate(date, VehicleConstant.vehicleTimeZone);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusDay(handleDate, 1));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d %H");
        } else if (type.equals(1)) {
            handleDate = AboutDateUtil.theSameDayOfDateAnother(date, VehicleConstant.vehicleTimeZone);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusDay(handleDate, 7));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        } else if (type.equals(2)) {
            handleDate = AboutDateUtil.theSameDayOfDateAnother(date, VehicleConstant.vehicleTimeZone);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusDay(handleDate, 30));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        } else if (type.equals(3)) {
            handleDate = AboutDateUtil.theSameMonthOfDate(date, VehicleConstant.vehicleTimeZone);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.dateMinusYear(handleDate, 1));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m");
        } else if (type.equals(-1) && null!=startTime && null!=endTime) {
            queryVehicleAccessRecordStatisticsBO.setStartTime(startTime);
            queryVehicleAccessRecordStatisticsBO.setEndTime(endTime);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        }
        //方式为今日
        else if(type.equals(4)){
            handleDate = AboutDateUtil.datePlusHour(AboutDateUtil.theSameDayHourOfDate(date, VehicleConstant.vehicleTimeZone),1);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.theSameDayOfDate(handleDate));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d %H");
        }
        //方式为本周
        else if(type.equals(5)){
            handleDate = AboutDateUtil.theNextDayOfDate(date,VehicleConstant.vehicleTimeZone);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.theFirstDayOfWeek(handleDate,VehicleConstant.vehicleTimeZone));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        }
        //方式为本月
        else if(type.equals(6)){
            handleDate = AboutDateUtil.theNextDayOfDate(date,VehicleConstant.vehicleTimeZone);
            queryVehicleAccessRecordStatisticsBO.setStartTime(AboutDateUtil.theSameMonthOfDate(handleDate,VehicleConstant.vehicleTimeZone));
            queryVehicleAccessRecordStatisticsBO.setEndTime(handleDate);
            queryVehicleAccessRecordStatisticsBO.setDateFormatString("%Y-%m-%d");
        }
        else {
            throw new BusinessException("查询车辆进出场统计，输入的参数type值非法");
        }

        queryVehicleAccessRecordStatisticsBO.setType(vehicleAccessRecordStatisticsDTO.getType());
        queryVehicleAccessRecordStatisticsBO.setProjectId(vehicleAccessRecordStatisticsDTO.getProjectId());
        queryVehicleAccessRecordStatisticsBO.setParkingLotName(vehicleAccessRecordStatisticsDTO.getParkingLotName());

        return queryVehicleAccessRecordStatisticsBO;
    }

}
